﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class UserPermissionsBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IRoleDA roleDa = DataAccess.CreateRoleDA();
        private static readonly IUserBillingentityDA userBillingentityDA = DataAccess.CreateUserBillingentityDA();
        private static readonly IUserCarrierDA userCarrierDA = DataAccess.CreateUserCarrierDA();
        private static readonly IUserCustomerDA userCustomerDA = DataAccess.CreateUserCustomerDA();
        private static readonly IUserOwnerDA userOwner = DataAccess.CreateUserOwnerDA();
        private static readonly IUserRoleDA userRole = DataAccess.CreateUserRoleDA();
        private static readonly IUserVendorDA userVendorDA = DataAccess.CreateUserVendorDA();
        private static readonly IUserWhDA userWh = DataAccess.CreateUserWhDA();

        public UserPermissionsBLL()
        {
            this.pubParasInfo = null;
            this.dataBaseID = new List<string>();
            this.dataBaseID.Add(ConstUtils.CONST_GLOBAL_TABLE);
        }

        public UserPermissionsBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public UserPermissionsBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (tempPubParasInfo != null)
            {
                this.pubParasInfo = new PubParasInfo();
                this.pubParasInfo = tempPubParasInfo;
            }
            this.dataBaseID = new List<string>();
            this.dataBaseID.Add(ConstUtils.CONST_GLOBAL_TABLE);
        }

        public string Delete(List<string> IDS)
        {
            int ids = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; IDS.Count > i; i++)
                {
                    ids += userWh.DeleteUserWh(dataBase, tran, IDS[i], this.dataBaseID);
                    ids += userOwner.DeleteUserOwner(dataBase, tran, IDS[i], this.dataBaseID);
                    ids += userRole.DeleteUserRole(dataBase, tran, IDS[i], this.dataBaseID);
                    ids += userCustomerDA.DeleteUserCustomer(dataBase, tran, IDS[i], this.dataBaseID[i]);
                    ids += userBillingentityDA.DeleteUserBillingentity(dataBase, tran, IDS[i], this.dataBaseID[i]);
                    ids += userCarrierDA.DeleteUserCarrier(dataBase, tran, IDS[i], this.dataBaseID[i]);
                    ids += userVendorDA.DeleteUserVendor(dataBase, tran, IDS[i], this.dataBaseID[i]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return ids.ToString();
        }

        public DataSet GetAllInfo()
        {
            throw new NotImplementedException();
        }

        public object GetInfoByID(string userID)
        {
            UserPermissionsInfo userPermissions = new UserPermissionsInfo();
            try
            {
                string strGlobal = commonMethod.GetDataBaseIDByWhDBName(ConstUtils.CONST_GLOBAL_SETTING);
                userPermissions.UserID = userID;
                userPermissions.AuthorizedWH = userWh.GetAuthorizedWHByUserID(userID);
                userPermissions.UnauthorizedWH = userWh.GetUnauthorizedWHByUserID(userID);
                userPermissions.UserRoleInfo = userRole.GetUserRoleByUserID(userID, this.dataBaseID[0]);
                userPermissions.AuthorizedOwner = userOwner.GetAuthorizedOwnerByUserID(userID, strGlobal);
                userPermissions.UnauthorizedOwner = userOwner.GetUnauthorizedOwnerByUserID(userID, strGlobal);
                userPermissions.AuthorizedBillingEntity = userBillingentityDA.GetAuthorizedBillingentityByUserID(userID, strGlobal);
                userPermissions.UnauthorizedBillingEntity = userBillingentityDA.GetUnauthorizedBillingentityByUserID(userID, strGlobal);
                userPermissions.AuthorizedCarrier = userCarrierDA.GetAuthorizedCarrierByUserID(userID, strGlobal);
                userPermissions.UnauthorizedCarrier = userCarrierDA.GetUnauthorizedCarrierByUserID(userID, strGlobal);
                userPermissions.AuthorizedCustomer = userCustomerDA.GetAuthorizedCustomerByUserID(userID, strGlobal);
                userPermissions.UnauthorizedCustomer = userCustomerDA.GetUnauthorizedCustomerByUserID(userID, strGlobal);
                userPermissions.AuthorizedVendor = userVendorDA.GetAuthorizedVendorByUserID(userID, strGlobal);
                userPermissions.UnauthorizedVendor = userVendorDA.GetUnauthorizedVendorByUserID(userID, strGlobal);
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return userPermissions;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            throw new NotImplementedException();
        }

        public string Insert(object obj)
        {
            throw new NotImplementedException();
        }

        public string Update(object obj)
        {
            string userID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            UserPermissionsInfo userPermissionInfo = obj as UserPermissionsInfo;
            try
            {
                userID = userPermissionInfo.UserID;
                userRole.DeleteUserRole(dataBase, tran, userID, this.dataBaseID);
                if (userPermissionInfo.UserRoleInfo.Count > 0)
                {
                    foreach (UserRoleInfo info in userPermissionInfo.UserRoleInfo)
                    {
                        if (!roleDa.CheckRoleIDUnique(dataBase, tran, info.RoleID, this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F1100_002", this.pubParasInfo.UserLanguage, new List<string> { info.RoleID });
                        }
                        info.CreatedBy = userID;
                        info.CreatedDate = DateTime.Now;
                        info.UpdatedBy = userID;
                        info.UpdatedDate = DateTime.Now;
                        userRole.InsertUserRole(dataBase, tran, info, this.dataBaseID);
                    }
                }
                foreach (DbConfigInfo info in userPermissionInfo.AuthorizedWH)
                {
                    userWh.DeleteUserWh(dataBase, tran, info.WhDbID.ToString(), userID, this.dataBaseID);
                    UserWhInfo userWhInfo = new UserWhInfo {
                        WhDbID = info.WhDbID.ToString(),
                        UserID = userID,
                        IsDefault = info.IsDefault,
                        CreatedBy = userID,
                        CreatedDate = DateTime.Now,
                        UpdatedBy = userID,
                        UpdatedDate = DateTime.Now
                    };
                    userWh.InsertUserWh(dataBase, tran, userWhInfo, this.dataBaseID);
                }
                foreach (DbConfigInfo info in userPermissionInfo.UnauthorizedWH)
                {
                    userWh.DeleteUserWh(dataBase, tran, info.WhDbID.ToString(), userID, this.dataBaseID);
                }
                foreach (OwnerInfo info in userPermissionInfo.AuthorizedOwner)
                {
                    userOwner.DeleteUserOwner(dataBase, tran, info.OwnerID, userID, this.dataBaseID);
                    UserOwnerInfo userOwnerInfo = new UserOwnerInfo {
                        OwnerID = info.OwnerID,
                        IsDefault = info.IsDefault,
                        UserID = userID,
                        CreatedBy = userID,
                        CreatedDate = DateTime.Now,
                        UpdatedBy = userID,
                        UpdatedDate = DateTime.Now
                    };
                    userOwner.InsertUserOwner(dataBase, tran, userOwnerInfo, this.dataBaseID);
                }
                foreach (OwnerInfo info in userPermissionInfo.UnauthorizedOwner)
                {
                    userOwner.DeleteUserOwner(dataBase, tran, info.OwnerID, userID, this.dataBaseID);
                }
                foreach (UserCustomerInfo info in userPermissionInfo.AuthorizedCustomer)
                {
                    userCustomerDA.DeleteUserCustomer(dataBase, tran, userID, info.CustomerID, this.dataBaseID[0]);
                    UserCustomerInfo userCustomerInfo = new UserCustomerInfo {
                        CustomerID = info.CustomerID,
                        IsDefault = info.IsDefault,
                        UserID = userID,
                        CreatedBy = userID,
                        CreatedDate = DateTime.Now,
                        UpdatedBy = userID,
                        UpdatedDate = DateTime.Now
                    };
                    userCustomerDA.InsertUserCustomer(dataBase, tran, userCustomerInfo, this.dataBaseID[0]);
                }
                foreach (UserCustomerInfo info in userPermissionInfo.UnauthorizedCustomer)
                {
                    userCustomerDA.DeleteUserCustomer(dataBase, tran, userID, info.CustomerID, this.dataBaseID[0]);
                }
                foreach (UserBillingentityInfo info in userPermissionInfo.AuthorizedBillingEntity)
                {
                    userBillingentityDA.DeleteUserBillingentity(dataBase, tran, userID, info.BillingentityID, this.dataBaseID[0]);
                    UserBillingentityInfo userBillingentityInfo = new UserBillingentityInfo {
                        BillingentityID = info.BillingentityID,
                        IsDefault = info.IsDefault,
                        UserID = userID,
                        CreatedBy = userID,
                        CreatedDate = DateTime.Now,
                        UpdatedBy = userID,
                        UpdatedDate = DateTime.Now
                    };
                    userBillingentityDA.InsertUserBillingentity(dataBase, tran, userBillingentityInfo, this.dataBaseID[0]);
                }
                foreach (UserBillingentityInfo info in userPermissionInfo.UnauthorizedBillingEntity)
                {
                    userBillingentityDA.DeleteUserBillingentity(dataBase, tran, userID, info.BillingentityID, this.dataBaseID[0]);
                }
                foreach (UserCarrierInfo info in userPermissionInfo.AuthorizedCarrier)
                {
                    userCarrierDA.DeleteUserCarrier(dataBase, tran, userID, info.CarrierID, this.dataBaseID[0]);
                    UserCarrierInfo userCarrierInfo = new UserCarrierInfo {
                        CarrierID = info.CarrierID,
                        IsDefault = info.IsDefault,
                        UserID = userID,
                        CreatedBy = userID,
                        CreatedDate = DateTime.Now,
                        UpdatedBy = userID,
                        UpdatedDate = DateTime.Now
                    };
                    userCarrierDA.InsertUserCarrier(dataBase, tran, userCarrierInfo, this.dataBaseID[0]);
                }
                foreach (UserCarrierInfo info in userPermissionInfo.UnauthorizedCarrier)
                {
                    userCarrierDA.DeleteUserCarrier(dataBase, tran, userID, info.CarrierID, this.dataBaseID[0]);
                }
                foreach (UserVendorInfo info in userPermissionInfo.AuthorizedVendor)
                {
                    userVendorDA.DeleteUserVendor(dataBase, tran, userID, info.VendorID, this.dataBaseID[0]);
                    UserVendorInfo userVendorInfo = new UserVendorInfo {
                        VendorID = info.VendorID,
                        IsDefault = info.IsDefault,
                        UserID = userID,
                        CreatedBy = userID,
                        CreatedDate = DateTime.Now,
                        UpdatedBy = userID,
                        UpdatedDate = DateTime.Now
                    };
                    userVendorDA.InsertUserVendor(dataBase, tran, userVendorInfo, this.dataBaseID[0]);
                }
                foreach (UserVendorInfo info in userPermissionInfo.UnauthorizedVendor)
                {
                    userVendorDA.DeleteUserVendor(dataBase, tran, userID, info.VendorID, this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return userID;
        }
    }
}

